#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <string.h>

void *tfn(void *arg){
    printf("thread: pid = %d, tid=%ld\n",getpid(),pthread_self());
    sleep(1);
    return NULL;
}



int main(int argc, char *argv[]){
    pthread_t tid;
    pthread_attr_t attr;
    int ret = pthread_attr_init(&attr);
    if (ret != 0){
        fprintf(stderr,"attr_init error %s\n",strerror(ret));
        exit(1);
    }
    ret = pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
    if (ret != 0){
        fprintf(stderr,"attr_setdetachstate error %s\n",strerror(ret));
        exit(1);
    }
    ret = pthread_create(&tid,&attr,tfn,NULL);
    if(ret !=0){
        fprintf(stderr,"attr_create error %s\n",strerror(ret));
        exit(1);
    }
    ret = pthread_attr_destroy(&attr);
    if(ret !=0){
        fprintf(stderr,"attr_destory error %s\n",strerror(ret));
        exit(1);
    }
    sleep(1);
    ret = pthread_join(tid,NULL);
    if(ret !=0){
        fprintf(stderr,"join error %s\n",strerror(ret));
        exit(1);
    }

    printf("main:pid = %d, tid = %lu\n",getpid(),pthread_self());

    pthread_exit((void*)0);

}